<?php

use com\gdl\newagk\interfaces\BulletinServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\bulletin\BulletinDTO;
use com\gdl\newagk\dataobject\bulletin\BulletinRequestDTO;


class api_BulletinService extends api_Abstract implements BulletinServiceIf
{

	//添加公告
    public function add(BulletinRequestDTO $basic)
    {
		$result = new ResultDO();
		$datas = $basic->filename ? $basic->filename : [];

		try{
			$result->code = 0;
			$result->success = true;
			/*****/

            if(empty($datas['content'])){
				 $result->message = "请填写公告内容";
				 return $result;
			}
			$bulletin = new model_newagk_bulletin();
			$platform = new model_newagk_platform();
			$platformDe = $platform->getInfoArg(['admin_reg'=>$datas['reg']],'id,reals');
			 if(empty($platformDe[0])){
				 $result->message = "发布失败";
				 return $result;
			}
				$data['content'] = $datas['content'];
				$data['status'] = $datas['status'];
				$data['platformId'] = $platformDe[0]['id'];
				$data['admin_reg'] = $datas['reg'];
				$data['platformName'] = $platformDe[0]['reals'];
				$data['release_time'] = date('Y-m-d H:i:s');
				$data['update_time'] = date('Y-m-d H:i:s');
				$bulletin->insert($data);
			/******/
				$result->message = '成功';
				//$result->message = json_encode($platformDe);
                $result->code = 1;


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

	//查看公告
	 public function lookOver(BulletinDTO $basic)
    {
			$result = new ResultDO();
			$id = $basic->id ? gdl_lib_BaseUtils::getStr($basic->id,'int') : 0;
			$reg = $basic->reg ? gdl_lib_BaseUtils::getStr($basic->reg,'string') : '';

		try{
			/**********/
			$result->code = 0;
            $result->success = true;
            if(!$id || $reg==''){
				$result->message = "缺少参数ID";
				return $result;
			}
			$paper = new model_newagk_bulletin();
			$paperArr = $paper->selectOne(['id'=>$id],'content,status');
			if(empty($paperArr)){
				$result->message = "记录不存在";
			} else {
				$result->code = 1;
				$data['content'] = $paperArr['content'];
				$data['status'] = $paperArr['status'];

				$result->data[] = $data;
			}
            /*********/


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	//修改公告
	 public function edit(BulletinRequestDTO $basic)
    {
		$result = new ResultDO();
		$datas = $basic->filename ? $basic->filename : [];
		$id = $basic->id ? gdl_lib_BaseUtils::getStr($basic->id,'int') : 0;
		try{
			$result->code = 0;
			$result->success = true;
			/*****/

            if(empty($datas['content'])){
				 $result->message = "请填写公告内容";
				 return $result;
			}
			$bulletin = new model_newagk_bulletin();
				$data['content'] = $datas['content'];
				$data['status'] = $datas['status'];
				$data['update_time'] = date('Y-m-d H:i:s');
				$bulletin->update('id='.$id,$data);
			/******/
				$result->message = '成功';
				//$result->message = json_encode($bulletin);
                $result->code = 1;


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}


	//公告列表
	 public function bulletinlist(BulletinRequestDTO $basic)
    {
		$result = new ResultDO();
		$filename = $basic->filename ? $basic->filename : [];

		$platform = $basic->filename['platform'] ?  gdl_lib_BaseUtils::getStr($basic->filename['platform'], 'string') : '';
		$title = $basic->filename['title'] ?  gdl_lib_BaseUtils::getStr($basic->filename['title'], 'string') : '';
		$status = $basic->filename['status'] ?  gdl_lib_BaseUtils::getStr($basic->filename['status'], 'string') : '';
		$paper = [];

		$reg = $basic->filename['reg'] ?  gdl_lib_BaseUtils::getStr($basic->filename['reg'], 'string') : '';
			//现在还没有启动平台id 所以下面只接差所有
			$modelPlaform = new model_newagk_platform();
			$bulletin = new model_newagk_bulletin();

			//$condition  = "admin_reg='{$reg}'";
			$condition  = "admin_reg!='' and isdelete=0";
			if($title!=''){
				$condition .= " and content like'%".$title."%'";
			}
			if($platform){
				$condition .= " and platformName like'%".$platform."%'";
			}

			//试卷状态
			if($status=='starts'){
				$condition .= ' and status=1';
			} else if($status=='disabled'){
				$condition .= ' and status=0';
			}


			/*
			if($platform){
				$reg = $modelPlaform->getChildAdminRegByPlaformId($platform_id,$platform);
				$condition .= " and admin_reg in('".implode("','",$reg)."')";
			}
			*/
			//////////////////
			$page = ($filename['page']-1) * $filename['pagesize'];

            $items = 'id,content,update_time,status,platformName';
            $groupBy = ' order by id desc  limit '.$page.','.$filename['pagesize'];

            try {
				$num = $bulletin->selectOne($condition, 'count(*) num');
                $res = $bulletin->select($condition, $items, $groupBy)->items;

                //进行平台顶级读取处理

                if (!empty($res)) {

                    $result->data = $res;

					$result->message = $num['num'];
                    $result->code = 1;

                } else {
                    $result->code = 0;
					 $result->message = 0;
                }
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;

	}


	public function examStatus(BulletinRequestDTO $Exam){
			 $result = new ResultDO();
        try {

			$filename = $Exam->filename ? $Exam->filename : [];
			$examid = $Exam->id ? explode(',',$Exam->id) : [];
			$reg  = $Exam->reg ? $Exam->reg : 0;

			$model = new model_newagk_bulletin();
			$id = [0];
			foreach($examid as $v){
				$id[] = (int)$v;
			}
			$condition = "id in('".implode("','",$id)."')";//'admin_reg'=>$reg,
			$res = 0;
			if(!empty($filename)){
				if(!empty($filename['isdelete']) && $filename['isdelete'] == 'removes'){
					$basicrule->delete($condition);
				} else {
					$res = $basic->update($condition,$filename);
				}
			}
            if ($res) {
                $result->code = 1;
				$result->message = '操作成功';
            } else {
                $result->code = 0;
				$result->message = '操作失败';
            }

            $result->success = true;
            return $result;
        } catch (Exception $e) {
            $result->success = false;

            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}


	//app公告列表 只显示最新几条
	 public function appList(BulletinRequestDTO $basic)
    {
		$result = new ResultDO();
		$platform_id = $basic->platform_id ? $basic->platform_id : 0;
		if(!$platform_id){
			$result->message = "请填写公告内容";
			return $result;
		}

			$num = 5;
            $items = 'id,content,update_time,status,platformName';
            $groupBy = ' order by id desc  limit 0,'.$num;
			$condition = '1';
            try {
                $res = $bulletin->select($condition, $items, $groupBy)->items;


                if (!empty($res)) {
					
                    $result->data = $res;
                    $result->code = 1;

                } else {
                    $result->code = 1;
				
                }
				$result->message = 0;
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;

	}
	
}
